package com.steven.ourcontest.net

import io.reactivex.Observable
import io.reactivex.ObservableSource
import io.reactivex.ObservableTransformer
import io.reactivex.Scheduler
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.lang.Exception


class ApiTransformer<T> : ObservableTransformer<BaseResponse<T>, T?> {
    override fun apply(upstream: Observable<BaseResponse<T>>): ObservableSource<T?> {
        return upstream.flatMap {
            if (it.errorCode == 0) {
                return@flatMap Observable.just(it.result)
            } else {
                println("error="+it.msg)
                throw Exception(it.msg)
            }
        }.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
    }

}